package de.tum.icuproject;

import de.tum.icuproject.MainActivity.SectionsPagerAdapter;
import de.tum.icuproject.model.Player;
import de.tum.icuproject.model.Session;
import de.tum.icuproject.persistence.PlayerRepository;
import de.tum.icuproject.persistence.couchdb.PlayerRepositoryImpl;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * The UpdatePlayerFragment provides fields to change player data and stores the changes to the database.
 * 
 */
public class UpdatePlayerFragment extends Fragment implements Subscriber {

	private SectionsPagerAdapter sectionsPagerAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.change_player,
				container, false);

		Player player = this.getCurrentPlayer();

		EditText editTextId = (EditText) rootView.findViewById(R.id.editTextChangeId);
		// do toString because this method is overloaded with integer
		editTextId.setText(player.getPlayerId().toString());
		EditText editTextFirstName = (EditText) rootView.findViewById(R.id.editTextChangeFirstName);
		editTextFirstName.setText(player.getFirstName());
		EditText editTextLastName = (EditText) rootView.findViewById(R.id.editTextChangeLastName);
		editTextLastName.setText(player.getLastName());
		EditText editTextBirth = (EditText) rootView.findViewById(R.id.editTextChangeBirthday);
		editTextBirth.setText(player.getBirthDate());
		EditText editTextHeight = (EditText) rootView.findViewById(R.id.editTextChangeHeight);
		editTextHeight.setText(String.valueOf(player.getHeight()));
		EditText editTextWeight = (EditText) rootView.findViewById(R.id.editTextChangeWeight);
		editTextWeight.setText(String.valueOf(player.getWeight()));
		RadioGroup radioGroupSex = (RadioGroup) rootView.findViewById(R.id.radioGroupChangeSex);
		if (player.isMale()) {
			radioGroupSex.check(R.id.RadioButtonChangeSex_Male);
		} else{
			radioGroupSex.check(R.id.RadioButtonChangeSex_Female);
		}

		Button btnStorePlayer = (Button) rootView.findViewById(R.id.btnChangeUser);
		btnStorePlayer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				storePlayer(v);
			}
		});

		Button btnReturn = (Button) rootView.findViewById(R.id.btnChangeUserReturn);
		btnReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				returnToPlayerFragment();
			}
		});

		return rootView;
	}

	public void storePlayer(View view) {

		EditText editTextName = (EditText) getView().findViewById(R.id.editTextChangeFirstName);
		String enteredName = editTextName.getText().toString();
		EditText editTextLastName = (EditText) getView().findViewById(R.id.editTextChangeLastName);
		String enteredLastName = editTextLastName.getText().toString();
		EditText editTextBirthday = (EditText) getView().findViewById(R.id.editTextChangeBirthday);
		String enteredBirthDate = editTextBirthday.getText().toString();
		EditText editTextHeight = (EditText) getView().findViewById(R.id.editTextChangeHeight);
		String enteredHeight = editTextHeight.getText().toString();
		EditText editTextWeight = (EditText) getView().findViewById(R.id.editTextChangeWeight);
		String enteredWeight = editTextWeight.getText().toString();

		RadioGroup radioGroupSex = (RadioGroup) getView().findViewById(R.id.radioGroupChangeSex);
		int checkedRadioBtnId = radioGroupSex.getCheckedRadioButtonId();
		RadioButton checkedRadioBtn = (RadioButton) getView().findViewById(checkedRadioBtnId);
		String radioBtnValue = checkedRadioBtn.getText().toString();	
		boolean isMale = true;
		if (radioBtnValue.equals(this.getString(R.string.sex_female))) {
			isMale = false;
		}

		if (!enteredName.equals("") && !enteredLastName.equals("") && !enteredBirthDate.equals("") && !enteredHeight.equals("") && !enteredWeight.equals("")) {
			try {
				int playerHeight = Integer.parseInt(enteredHeight);
				int playerWeight = Integer.parseInt(enteredWeight);
				MainActivity mainActivity = (MainActivity) getActivity();
				// Get current player from activity:
				Player player = mainActivity.getCurrentPlayer();
				// Set all eventually modified fields:
				player.setFirstName(enteredName);
				player.setLastName(enteredLastName);
				player.setBirthDate(enteredBirthDate);
				player.setHeight(playerHeight);
				player.setWeight(playerWeight);
				player.setIsMale(isMale);
				PlayerRepository playerRepo = new PlayerRepositoryImpl(this);
				playerRepo.update(player);
				Toast.makeText(getView().getContext(), "Please wait...", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				Toast.makeText(getView().getContext(), "The input is of a wrong format. Please check again!", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getView().getContext(), "No field can be empty!", Toast.LENGTH_SHORT).show();
		} 
	}

	@Override
	public void setCurrentPlayer(Player player) {
	}

	@Override
	public void setPlayerId(int playerId) {
	}

	@Override
	public void confirmInsertion(boolean confirm) {
	}

	/** 
	 * Gives a confirmation of the update or an error if necessary.
	 * 
	 * @param confirm
	 */
	@Override
	public void confirmUpdate(boolean confirm) {
		Log.d("CONFIRM_UPDATE", String.valueOf(confirm));
		if (confirm){
			Toast.makeText(getActivity(), "Updated player? = " + confirm, Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(getActivity(), "Error, please try again.", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void confirmDeletion(boolean confirm) {
	}

	@Override
	public void setSessionId(int sessionId) {
	}

	/**
	 * Returns to the PlayerFragment.
	 */
	public void returnToPlayerFragment() {
		PlayerFragment playerFragment = new PlayerFragment();
		playerFragment.setSectionsPagerAdapter(sectionsPagerAdapter);
		sectionsPagerAdapter.updatePlayerFragment(playerFragment);
	}

	public void setSectionsPagerAdapter(
			SectionsPagerAdapter sectionsPagerAdapter) {
		this.sectionsPagerAdapter = sectionsPagerAdapter;		
	}

	@Override
	public void setCurrentSession(Session session) {
	}

	@Override
	public Player getCurrentPlayer() {
		MainActivity mainActivity = (MainActivity) getActivity();
		return mainActivity.getCurrentPlayer();
	}

}
